#include <stdio.h>
#include "dclcd.h"
#include "tskcfg.h"
#include "bsp.h"

static void keyt(void);
static void set(void);
static void menu(void);
static void keyret(void);
static void down(void);
static void up(void);
static void left(void);
static void right(void);
static void ok(void);

static void load(uint8_t nowpage, uint8_t nowfocus);
static void update(uint8_t keyvalue);

#define MAX_FOCUS        3

static const uint8_t ssid = 1;
static volatile uint8_t focus = 0;

const static GUI_FormKeysStru gui_state_keys = 
{
  .mute =   NULL,
  .power =  NULL,
  .keyt =   NULL,
  .set =    NULL,
  .home =   gui_form_common_home,
  .menu =   menu,
  .keyret = keyret,
  .enter =  NULL,
  .down =   down,
  .up =     up,
  .left =   up,
  .right =  down,
  .ok =     NULL,
  .volinc = NULL,
  .voldec = NULL,
  .key0 =   NULL,
  .key1 =   NULL,
  .key2 =   NULL,
  .key3 =   NULL,
  .key4 =   NULL,
  .key5 =   NULL,
  .key6 =   NULL,
  .key7 =   NULL,
  .key8 =   NULL,
  .key9 =   NULL,
};

const GUI_FormStru gui_from_state = 
{
  .load    = load,
  .update  = update,
  .keys    = &gui_state_keys,
  .refresh = &gui_mainmenu_refresh_time,
};

static void load(uint8_t nowpage, uint8_t nowfocus)
{
  focus = 0;
  dc_cmd_change_screen(ssid);
}

static void update(uint8_t keyvalue)
{
  switch(focus)
  {
    default:
      focus = 0;
    case 0:
      dc_display_state_pg1();
      break;

    case 1:
      dc_display_state_pg2();
      break;

    case 2:
      dc_display_state_pg3();
      break;

    case 3:
      dc_display_state_pg4();
      break;
  }
}

static void menu(void)
{
//  gui_form_exec(&Gui_FrmSetting0);
}

static void keyret(void)
{
  gui_form_exec(&gui_from_main, 0, 0);
}

static void down(void)
{
  if(focus >= MAX_FOCUS)
  {
    focus = 0;
  }
  else
  {
    focus++;
  }
}

static void up(void)
{
  if(focus > MAX_FOCUS)
  {
    focus = MAX_FOCUS;
  }
  if(focus <= 0)
  {
    focus = MAX_FOCUS;
  }
  else
  {
    focus--;
  }
}

static void ok(void)
{
}
